package pong;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Circle;

import states.SlickGame;
import globals.DataTracker;
import globals.Vector2D;
import interfaces.Trackable;

public class Ball extends Trackable{
	// float x,y,r from Trackable
	public Color colorMe=purple;
	public Vector2D direction;
	public int timer=0;
	public boolean outOfRoom=false;
	
	// Globals.
	public static Color purple=new Color(87,0,127); // purple
	
	public static float ballMagLimit=25;
	public static float timeTilMagInc=SlickGame.fps*50;
	public boolean collision_type=true; // Collisions, True: Pong Standard, False: Angle
	public static int xDepth=12; // depth in x axis for screen overflow.
	
	public Ball(float xpos, float ypos, float rval, boolean left){
		super(xpos, ypos, rval);
		if(left){
			direction=new Vector2D(-1,0);
		}else{
			direction=new Vector2D(1,0);
		}
		direction.setMagnatude(4);
	}
	
	public void update(int delta){
		timer+=delta;
		float curMag=direction.getMagnatude();
		
		outOfRoom=false;
		if(!collision_type){ // Standard
			if(x<-xDepth*r){
				Vector2D cp=direction.getCopy();
				cp.setAngle(0);
				direction.add(cp);
				outOfRoom=true;
			}
			else if(x>SlickGame.width+xDepth*r){
				Vector2D cp=direction.getCopy();
				cp.setAngle((float) Math.PI);
				direction.add(cp);
				outOfRoom=true;
			}
			if(y<0){
				Vector2D cp=direction.getCopy();
				cp.setAngle((float)(Math.PI/2));
				direction.add(cp);
			}
			else if(y>SlickGame.height){
				Vector2D cp=direction.getCopy();
				cp.setAngle((float)(3*Math.PI/2));
				direction.add(cp);
			}
		}
		else{ // Pong Standard
			if(x<-xDepth*r){
				direction.setX(direction.getX()*-1);
				outOfRoom=true;
			}
			else if(x>SlickGame.width+xDepth*r){
				direction.setX(direction.getX()*-1);
				outOfRoom=true;
			}
			if(y<0){
				direction.setY(direction.getY()*-1);
			}
			else if(y>SlickGame.height){
				direction.setY(direction.getY()*-1);
			}
		}
		
		if(timer>timeTilMagInc){
			timer=0;
			direction.setMagnatude(curMag+1);
		}else{direction.setMagnatude(curMag);}
		if(curMag>ballMagLimit){direction.setMagnatude(ballMagLimit);}
		
		x+=direction.getX();
		y+=direction.getY();
		
	}
	
	public Ball getCopy(){
		Ball copy=new Ball(x, y, r,true);
		copy.direction=this.direction.getCopy();
		
		return copy;
	}
	
	public void draw(Graphics g){
		g.setColor(colorMe.brighter());
		g.fill(new Circle(x,y,r));
		g.setColor(colorMe);
		g.draw(new Circle(x,y,r));
	}
	
}
